package systempen.cmanager;

import java.util.List;
import systempen.cmanager.R;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CostListAdapter extends ArrayAdapter<CostListItem> {

	private List<CostListItem> items;

	public CostListAdapter(Context context, int textViewResourceId, List<CostListItem> items) {
		super(context, textViewResourceId, items);
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		//Typeface font = Typeface.createFromAsset(getContext().getAssets(), "Tuffy.ttf");
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.costlist_row, null);
		}
		CostListItem o = items.get(position);
		if (o != null) {
			TextView tg = (TextView) v.findViewById(R.id.ctgornji);
			if (tg != null) {
				tg.setText(o.getGornji());
				//		tg.setTypeface(font);
			}
			TextView td = (TextView) v.findViewById(R.id.ctdonji);
			if (td != null) {
				td.setText(o.getDonji());
				//		td.setTypeface(font);
			}
			TextView tc = (TextView) v.findViewById(R.id.ctcode);
			if (tc != null) {
				tc.setText(o.getCode());
				//		tc.setTypeface(font);
			}
			TextView tcif = (TextView) v.findViewById(R.id.cifra);
			String cifra = "";
			if (tcif != null) {
				cifra = o.getCifra();
				if (cifra.contains("-")) {
					tcif.setTextColor(getContext().getResources().getColor(R.color.ColOrange));
					tcif.setText(cifra.substring(1)); //trim minus
				} else {
					tcif.setTextColor(getContext().getResources().getColor(R.color.ColMoney));
					tcif.setText(cifra);
				}
			}
			TextView ttype = (TextView) v.findViewById(R.id.ctType);
			if (ttype != null) {
				ttype.setText(o.getType());
				//		ttype.setTypeface(font);
			}
			TextView tpath = (TextView) v.findViewById(R.id.ctPath);
			if (tpath != null) {
				tpath.setText(o.getPath());
				//		tpath.setTypeface(font);
			}
			
			TextView tcumulativeSum = (TextView) v.findViewById(R.id.CumulativeSum);
			if (tcumulativeSum != null)
			{
				if (o.getCumulativeSum() != null)
				{
					if (o.getCumulativeSum().subSequence(0, 1).equals("-"))
					{
						tcumulativeSum.setText("Balance: "+o.getCumulativeSum().substring(1));
					}
					else
						tcumulativeSum.setText("Balance: "+o.getCumulativeSum());
					
					String s = null;
					s = o.getCumulativeSum().toString().substring(0, 1);
					if (o.getCumulativeSum().toString().substring(0, 1).equals("-"))
						tcumulativeSum.setTextColor(getContext().getResources().getColor(R.color.ColGreen2));
					else
						tcumulativeSum.setTextColor(getContext().getResources().getColor(R.color.ColRed));
				}
			}
			

			/*
			 * ImageView im; im = (ImageView)v.findViewById(R.id.imageFolder);
			 * if (util.existsChildCost(tc.getText().toString(), getContext())){
			 * im.setImageResource(R.drawable.folder); }else{
			 * im.setImageBitmap(null); }
			 */

			TextView tOpis = (TextView) v.findViewById(R.id.opis);
			if (tOpis != null) {
				tOpis.setText(o.getOpis());
				if (tOpis.getText().equals("No description")){
					tOpis.setText("");
				}
			}
			TextView tPictureParam = (TextView) v.findViewById(R.id.picture_param);
			if (tPictureParam != null){
				if(o.getPicture()!=null && !o.getPicture().equals("")){
					tPictureParam.setText("+Picture");
				}else{
					tPictureParam.setText("");
				}
			}


			TextView tOpis2 = (TextView) v.findViewById(R.id.opis2);
			if (tOpis2 != null) {
				tOpis2.setText(o.getOpis2());
				if (tOpis2.getText().equals("No description")){
					tOpis2.setText("");
				}
			}

		}
		return v;
	}
}